# Maintainer: Levente Polyak <anthraxx[at]archlinux[dot]org>
# Contributor: Chocobo1 <chocobo1 AT archlinux DOT net>
# Contributor: Jacek Szafarkiewicz <szafar at linux dot pl>

pkgbase=zlib-ng
pkgname=(zlib-ng)
pkgver=2.1.4
pkgrel=1
pkgdesc='zlib replacement with optimizations for next generation systems'
url='https://github.com/zlib-ng/zlib-ng'
arch=('loong64' 'x86_64')
license=('custom:zlib')
depends=(
  glibc
)
makedepends=(
  cmake
  ninja
)
source=("${url}/archive/refs/tags/$pkgver/${pkgbase}-${pkgver}.tar.gz")
sha256sums=('a0293475e6a44a3f6c045229fe50f69dc0eebc62a42405a51f19d46a5541e77a')
b2sums=('0b5aff9671b43bda2adfc84987c6baa108fa444978d465f12516f13f32b88e6bbe52f7d3e48ff93c720229842062fe71c38ff7c1e7591e18fc7351c943fd2a6b')


build() {
  cd "${pkgbase}-${pkgver}"

	# WITH_UNALIGNED - unaligned access invokes undefined behaviour,
	#   see https://github.com/gentoo/gentoo/pull/17167 for more info.
  local _options=(
    -G Ninja
    -DCMAKE_BUILD_TYPE=None
    -DCMAKE_INSTALL_PREFIX=/usr
    -DCMAKE_INSTALL_LIBDIR=lib
    -Wno-dev
    -DWITH_GTEST=OFF
    -DWITH_UNALIGNED=OFF
  )

  msg2 "Building zlib-ng"
  cmake -B build \
    "${_options[@]}"
  cmake --build build

  msg2 "Building zlib-ng-compat"
  cmake -B build-compat \
    "${_options[@]}" \
    -DZLIB_COMPAT=ON
  cmake --build build-compat
}

check() {
  cd "${pkgbase}-${pkgver}"
  msg2 "Checking zlib-ng"
  ctest --output-on-failure --test-dir build
  msg2 "Checking zlib-ng-compat"
  ctest --output-on-failure --test-dir build-compat
}

package_zlib-ng() {
  cd "${pkgbase}-${pkgver}"
  DESTDIR="${pkgdir}" cmake --install build
  install -Dm 644 LICENSE.md -t "${pkgdir}/usr/share/licenses/${pkgname}"
  install -Dm 644 README.md -t "${pkgdir}/usr/share/doc/${pkgname}"
}

package_zlib-ng-compat() {
  pkgdesc+=" (zlib compat)"
  provides=(zlib)
  conflicts=(zlib)

  cd "${pkgbase}-${pkgver}"
  DESTDIR="${pkgdir}" cmake --install build-compat
  install -Dm 644 LICENSE.md -t "${pkgdir}/usr/share/licenses/${pkgname}"
  install -Dm 644 README.md -t "${pkgdir}/usr/share/doc/${pkgname}"
}

# vim: ts=2 sw=2 et:
